/**
 * 
 */
package com.ksv.lop;

import java.util.ArrayList;

import com.ksv.qlsv.HocKyDeleteActivity;
import com.ksv.qlsv.LopHocDeleteActivity;
import com.ksv.qlsv.MonHocDeleteActivity;
import com.ksv.qlsv.R;
import com.ksv.qlsv.SinhVienDeleteActivity;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class NewAdapter extends BaseAdapter {

	private Context mContext;
	public ArrayList<GetData> mArrGetData;
	public static String mID;
	public static String mName;
	public static int mIndex = 1;

	public NewAdapter(Context context, Cursor cur) {
		// TODO Auto-generated constructor stub
		mContext = context;
		mArrGetData = getDataFromCursor(cur);
	}
	
	private ArrayList<GetData> getDataFromCursor(Cursor c){
		ArrayList<GetData> mang = new ArrayList<GetData>();
		c.moveToFirst();
		while (c.getPosition() >= 0) {
			GetData data = new GetData();
			data.setID(c.getInt(c.getColumnIndex(mID)));
			data.setName(c.getString(c.getColumnIndex(mName)));
			mang.add(data);
			if(c.getPosition() >= c.getCount() - 1)
				break;
			c.moveToNext();
		}
		return mang;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArrGetData.size();
	}

	@Override
	public GetData getItem(int position) {
		// TODO Auto-generated method stub
		return mArrGetData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return mArrGetData.get(position).getID();
	}

	@Override
	public View getView(int position, View view, ViewGroup viewGroup) {
		// TODO Auto-generated method stub
		LayoutInflater layoutInflater = LayoutInflater.from(mContext);
		LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.listrow_del, null);
		TableRow tblrow = (TableRow) linearLayout.findViewById(R.id.tblrow_monhoc);
		TextView txt = (TextView) tblrow.findViewById(R.id.txt_name);
		txt.setText(mArrGetData.get(position).getName());
		CheckBox check = (CheckBox) tblrow.findViewById(R.id.check);
		check.setChecked(mArrGetData.get(position).getChecked());
		check.setId(position);
		check.setOnCheckedChangeListener(new OnCheckedChangeListener() {			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				try{
					int index = (int)buttonView.getId();
					if(isChecked == true){
						mArrGetData.get(index).setChecked(true);
					}
					else{
						mArrGetData.get(index).setChecked(false);
					}
					boolean checked = false;
					for (int i = 0; i < mArrGetData.size(); i++) {
						if(mArrGetData.get(i).getChecked() == true){
							if(mIndex == 2){
								LopHocDeleteActivity.btn_xoa.setEnabled(true);
							}
							else if(mIndex == 3){
								MonHocDeleteActivity.btn_xoa.setEnabled(true);
							}
							else if(mIndex == 4){
								HocKyDeleteActivity.btn_xoa.setEnabled(true);
							}
							else if(mIndex == 5){
								SinhVienDeleteActivity.btn_xoa.setEnabled(true);
							}
							checked =true;
							break;
						}
					}
					if(checked == false){
						if(mIndex == 2){
							LopHocDeleteActivity.btn_xoa.setEnabled(false);
						}
						else if(mIndex == 3){
							MonHocDeleteActivity.btn_xoa.setEnabled(false);
						}
						else if(mIndex == 4){
							HocKyDeleteActivity.btn_xoa.setEnabled(false);
						}
						else if(mIndex == 5){
							SinhVienDeleteActivity.btn_xoa.setEnabled(false);
						}
					}
				}
				catch(Exception Ex){
					
				}
			}
		});
		return linearLayout;
	}
	
	public void setCheckedAll(){
		for (int i = 0; i < mArrGetData.size(); i++) {
			mArrGetData.get(i).setChecked(true);
		}
		this.notifyDataSetChanged();
	}
	
	public void setUnCheckedAll(){
		for (int i = 0; i < mArrGetData.size(); i++) {
			mArrGetData.get(i).setChecked(false);
		}
		this.notifyDataSetChanged();
	}
	
	public void Add(GetData data){
		mArrGetData.add(data);
		this.notifyDataSetChanged();
	}
	
	public void Clear(){
		mArrGetData.clear();
		this.notifyDataSetChanged();
	}
	
	public void Remove(int index){
		mArrGetData.remove(index);
		this.notifyDataSetChanged();
	}

}
